/*
 * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
 *
 * SPDX-License-Identifier: Unlicense OR CC0-1.0
 */
#include <stdio.h>
#include <freertos/FreeRTOS.h>

#include "unity.h"
#if __has_include("esp_random.h")
#include "esp_random.h"
#else
#include "esp_system.h"
#endif
#include "esp_encrypted_img.h"

extern const uint8_t rsa_private_pem_start[] asm("_binary_test_rsa_private_key_pem_start");
extern const uint8_t rsa_private_pem_end[]   asm("_binary_test_rsa_private_key_pem_end");

extern const uint8_t bin_start[] asm("_binary_image_bin_start");
extern const uint8_t bin_end[]   asm("_binary_image_bin_end");

TEST_CASE("Sending all data at once", "[encrypted_img]")
{
    esp_decrypt_cfg_t cfg = {
        .rsa_priv_key = (char *)rsa_private_pem_start,
        .rsa_priv_key_len = rsa_private_pem_end - rsa_private_pem_start,
    };
    esp_decrypt_handle_t ctx = esp_encrypted_img_decrypt_start(&cfg);
    TEST_ASSERT_NOT_NULL(ctx);

    pre_enc_decrypt_arg_t *args = calloc(1, sizeof(pre_enc_decrypt_arg_t));
    TEST_ASSERT_NOT_NULL(args);

    args->data_in = (char *)bin_start;
    args->data_in_len = bin_end - bin_start;

    esp_err_t err;
    err = esp_encrypted_img_decrypt_data(ctx, args);

    TEST_ESP_OK(err);
    printf("Successful\n");
    printf("\n");

    err = esp_encrypted_img_decrypt_end(ctx);
    TEST_ESP_OK(err);
    if (args->data_out) {
        free(args->data_out);
    }
    free(args);
}

TEST_CASE("Sending 1 byte data at once", "[encrypted_img]")
{
    esp_decrypt_cfg_t cfg = {
        .rsa_priv_key = (char *)rsa_private_pem_start,
        .rsa_priv_key_len = rsa_private_pem_end - rsa_private_pem_start,
    };
    esp_decrypt_handle_t ctx = esp_encrypted_img_decrypt_start(&cfg);
    TEST_ASSERT_NOT_NULL(ctx);

    pre_enc_decrypt_arg_t *args = calloc(1, sizeof(pre_enc_decrypt_arg_t));
    TEST_ASSERT_NOT_NULL(args);

    esp_err_t err;
    int i = 0;
    do {
        args->data_in = (char *)(bin_start + i);
        i++;
        args->data_in_len = 1;
        err = esp_encrypted_img_decrypt_data(ctx, args);
        if (err == ESP_FAIL) {
            printf("ESP_FAIL ERROR\n");
            break;
        }
    } while (err != ESP_OK);
    TEST_ESP_OK(err);

    err = esp_encrypted_img_decrypt_end(ctx);
    TEST_ESP_OK(err);
    if (args->data_out) {
        free(args->data_out);
    }
    free(args);
}

TEST_CASE("Invalid Magic", "[encrypted_img]")
{
    uint8_t cipher[] = {
        0x34, 0x12, 0xbc, 0xec,
        0xf5, 0x3a, 0x72, 0x55, 0x36, 0x0f, 0x4a, 0x92,
        0x5f, 0x15, 0xef, 0xbb, 0xe8, 0x9c, 0x7e, 0x43,
        0x7c, 0x87, 0x3d, 0x00, 0xb4, 0x22, 0xcc, 0x35,
        0x77, 0x92, 0x6a, 0x44, 0x80, 0x83, 0x9d, 0x0d,
        0x4c, 0x28, 0x5b, 0x7d, 0xbd, 0xef, 0x59, 0x56,
        0xba, 0x0f, 0x9d, 0xab, 0x3b, 0x09, 0x26, 0x77,
        0x82, 0x28, 0x24, 0x6e, 0xb8, 0x35, 0x96, 0x87,
        0xd6, 0x35, 0x03, 0x9b, 0x4e, 0x60, 0xaf, 0x11,
        0x14, 0x74, 0x13, 0x75, 0xb1, 0xf6, 0x5b, 0xe5,
        0xb9, 0xf0, 0x2b, 0x37, 0x02, 0xaf, 0x76, 0x8f,
        0xe1, 0x08, 0xfa, 0x46, 0xa1, 0xb0, 0x8d, 0x04,
        0xb6, 0x09, 0x8b, 0x0f, 0x8c, 0x54, 0xab, 0x9d,
        0xe9, 0x32, 0xb7, 0xae, 0xc5, 0x6d, 0x5d, 0x85,
        0x54, 0x2b, 0x1e, 0x8c, 0xbe, 0x4c, 0xee, 0x89,
        0xc4, 0x16, 0x97, 0x6c, 0x48, 0xaf, 0x3c, 0xef,
        0x90, 0x01, 0x12, 0xd6, 0x72, 0xf4, 0xca, 0xc5,
        0xa1, 0xec, 0x8d, 0xe0, 0x4b, 0xc8, 0xb2, 0xb4,
        0x66, 0x92, 0xd8, 0x0f, 0x65, 0x4d, 0x16, 0x30,
        0x6d, 0x2e, 0x3b, 0x64, 0x22, 0x36, 0x02, 0x43,
        0x10, 0xa8, 0x77, 0xb1, 0xf2, 0x8c, 0x29, 0x8d,
        0x23, 0xf2, 0xc6, 0xd4, 0x5b, 0x2e, 0x78, 0x15,
        0x8b, 0x5e, 0x0a, 0x2b, 0xd8, 0x76, 0x83, 0xe6,
        0xf9, 0x2a, 0xad, 0xf7, 0xf1, 0x06, 0x18, 0xbf,
        0xe1, 0x50, 0x89, 0xbf, 0x92, 0xfd, 0xe8, 0xcf,
        0x06, 0x5b, 0xf3, 0xa0, 0x3b, 0xa2, 0x60, 0x0e,
        0x04, 0xc8, 0x80, 0xb7, 0x97, 0xce, 0xdc, 0xc6,
        0x7c, 0xdb, 0x82, 0x77, 0xa7, 0x84, 0xf3, 0xf3,
        0x4a, 0xfa, 0xce, 0x9a, 0x1e, 0x4b, 0x81, 0x45,
        0xed, 0xfc, 0xd7, 0xe3, 0x4d, 0x40, 0x03, 0x8f,
        0x17, 0x30, 0x8f, 0x35, 0x62, 0xc3, 0x9e, 0x1d,
        0x11, 0xee, 0x3c, 0x5a, 0xbd, 0x3b, 0x32, 0x6c,
        0x16, 0xef, 0xab, 0x67, 0xe7, 0x57, 0x89, 0x61,
        0xbd, 0x1c, 0xb0, 0x28, 0x58, 0x3b, 0x70, 0xd7,
        0x4a, 0x8e, 0x08, 0x0b, 0x3b, 0x14, 0x15, 0x7c,
        0x0e, 0x97, 0xcc, 0xa6, 0x00, 0xf7, 0x0c, 0xa0,
        0x3a, 0xd0, 0xc7, 0x97, 0xbe, 0xf6, 0xc6, 0xe7,
        0xc4, 0xc4, 0x90, 0x3d, 0x94, 0xf8, 0xd3, 0x71,
        0x85, 0x6c, 0x40, 0x97, 0xae, 0x52, 0x0e, 0xe0,
        0x70, 0x15, 0x13, 0xe4, 0xcd, 0xbb, 0xf0, 0x80,
        0x11, 0x56, 0x08, 0xff, 0x71, 0xa3, 0xca, 0x00,
        0x43, 0x0d, 0xde, 0xa1, 0x81, 0xaa, 0x52, 0x9a,
        0xfd, 0x64, 0x58, 0x6c, 0x99, 0x37, 0x9b, 0x04,
        0x46, 0x33, 0x22, 0x88, 0x53, 0x1a, 0x99, 0x54,
        0xf2, 0x77, 0x00, 0x23, 0xf0, 0xf3, 0x24, 0x02,
        0x7d, 0x2a, 0x93, 0x98, 0x25, 0x5f, 0x55, 0xaf,
        0x99, 0xf6, 0x69, 0x4e, 0x3a, 0xdf, 0x8f, 0xed,
        0x59, 0x91, 0xaa, 0xff, 0x0b, 0xaa, 0x70, 0x8e,
        0x08, 0xfe, 0xa1, 0x32, 0xa9, 0xa7, 0xc6, 0xf9,
        0x1d, 0xfa, 0x52, 0x32, 0xc5, 0x5b, 0x64, 0x1e,
        0x75, 0x67, 0xe0, 0xac, 0xe0, 0x35, 0x9d, 0x4a,
        0x09, 0x00, 0x00, 0x00, 0x00, 0xb5, 0x9a, 0xf1,
        0xe2, 0x4a, 0xe8, 0xea, 0xed, 0x68, 0x15, 0xf4,
        0x04, 0x36, 0x96, 0x60, 0x48, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x5a, 0x2f, 0x2a, 0x16,
        0x6c, 0x8b, 0x34, 0x4e, 0xc0
    };

    esp_decrypt_cfg_t cfg = {
        .rsa_priv_key = (char *)rsa_private_pem_start,
        .rsa_priv_key_len = rsa_private_pem_end - rsa_private_pem_start,
    };
    esp_decrypt_handle_t ctx = esp_encrypted_img_decrypt_start(&cfg);
    TEST_ASSERT_NOT_NULL(ctx);

    pre_enc_decrypt_arg_t *args = calloc(1, sizeof(pre_enc_decrypt_arg_t));
    TEST_ASSERT_NOT_NULL(args);

    args->data_in = (char *)cipher;
    args->data_in_len = 521;

    esp_err_t err;

    err = esp_encrypted_img_decrypt_data(ctx, args);

    TEST_ESP_ERR(ESP_FAIL, err);

    err = esp_encrypted_img_decrypt_end(ctx);
    free(args);
}

TEST_CASE("Invalid Image", "[encrypted_img]")
{
    //"Espressif" is encoded using GCM key. After successful decoding, "Espressif" will be printed.
    uint8_t cipher[] = {
        0xcf, 0xb6, 0x88, 0x07,
        0xf5, 0x3a, 0x72, 0x55, 0x36, 0x0f, 0x4a, 0x92,
        0x5f, 0x15, 0xef, 0xbb, 0xe8, 0x9c, 0x7e, 0x43,
        0x7c, 0x87, 0x3d, 0x00, 0xb4, 0x22, 0xcc, 0x35,
        0x77, 0x92, 0x6a, 0x44, 0x80, 0x83, 0x9d, 0x0d,
        0x4c, 0x28, 0x5b, 0x7d, 0xbd, 0xef, 0x59, 0x56,
        0xba, 0x0f, 0x9d, 0xab, 0x3b, 0x09, 0x26, 0x77,
        0x82, 0x28, 0x24, 0x6e, 0xb8, 0x35, 0x96, 0x87,
        0xd6, 0x35, 0x03, 0x9b, 0x4e, 0x60, 0xaf, 0x11,
        0x14, 0x74, 0x13, 0x75, 0xb1, 0xf6, 0x5b, 0xe5,
        0xb9, 0xf0, 0x2b, 0x37, 0x02, 0xaf, 0x76, 0x8f,
        0xe1, 0x08, 0xfa, 0x46, 0xa1, 0xb0, 0x8d, 0x04,
        0xb6, 0x09, 0x8b, 0x0f, 0x8c, 0x54, 0xab, 0x9d,
        0xe9, 0x32, 0xb7, 0xae, 0xc5, 0x6d, 0x5d, 0x85,
        0x54, 0x2b, 0x1e, 0x8c, 0xbe, 0x4c, 0xee, 0x89,
        0xc4, 0x16, 0x97, 0x6c, 0x48, 0xaf, 0x3c, 0xef,
        0x90, 0x01, 0x12, 0xd6, 0x72, 0xf4, 0xca, 0xc5,
        0xa1, 0xec, 0x8d, 0xe0, 0x4b, 0xc8, 0xb2, 0xb4,
        0x66, 0x92, 0xd8, 0x0f, 0x65, 0x4d, 0x16, 0x30,
        0x6d, 0x2e, 0x3b, 0x64, 0x22, 0x36, 0x02, 0x43,
        0x10, 0xa8, 0x77, 0xb1, 0xf2, 0x8c, 0x29, 0x8d,
        0x23, 0xf2, 0xc6, 0xd4, 0x5b, 0x2e, 0x78, 0x15,
        0x8b, 0x5e, 0x0a, 0x2b, 0xd8, 0x76, 0x83, 0xe6,
        0xf9, 0x2a, 0xad, 0xf7, 0xf1, 0x06, 0x18, 0xbf,
        0xe1, 0x50, 0x89, 0xbf, 0x92, 0xfd, 0xe8, 0xcf,
        0x06, 0x5b, 0xf3, 0xa0, 0x3b, 0xa2, 0x60, 0x0e,
        0x04, 0xc8, 0x80, 0xb7, 0x97, 0xce, 0xdc, 0xc6,
        0x7c, 0xdb, 0x82, 0x77, 0xa7, 0x84, 0xf3, 0xf3,
        0x4a, 0xfa, 0xce, 0x9a, 0x1e, 0x4b, 0x81, 0x45,
        0xed, 0xfc, 0xd7, 0xe3, 0x4d, 0x40, 0x03, 0x8f,
        0x17, 0x30, 0x8f, 0x35, 0x62, 0xc3, 0x9e, 0x1d,
        0x11, 0xee, 0x3c, 0x5a, 0xbd, 0x3b, 0x32, 0x6c,
        0x16, 0xef, 0xab, 0x67, 0xe7, 0x57, 0x89, 0x61,
        0xbd, 0x1c, 0xb0, 0x28, 0x58, 0x3b, 0x70, 0xd7,
        0x4a, 0x8e, 0x08, 0x0b, 0x3b, 0x14, 0x15, 0x7c,
        0x0e, 0x97, 0xcc, 0xa6, 0x00, 0xf7, 0x0c, 0xa0,
        0x3a, 0xd0, 0xc7, 0x97, 0xbe, 0xf6, 0xc6, 0xe7,
        0xc4, 0xc4, 0x90, 0x3d, 0x94, 0xf8, 0xd3, 0x71,
        0x85, 0x6c, 0x40, 0x97, 0xae, 0x52, 0x0e, 0xe0,
        0x70, 0x15, 0x13, 0xe4, 0xcd, 0xbb, 0xf0, 0x80,
        0x11, 0x56, 0x08, 0xff, 0x71, 0xa3, 0xca, 0x00,
        0x43, 0x0d, 0xde, 0xa1, 0x81, 0xaa, 0x52, 0x9a,
        0xfd, 0x64, 0x58, 0x6c, 0x99, 0x37, 0x9b, 0x04,
        0x46, 0x33, 0x22, 0x88, 0x53, 0x1a, 0x99, 0x54,
        0xf2, 0x77, 0x00, 0x23, 0xf0, 0xf3, 0x24, 0x02,
        0x7d, 0x2a, 0x93, 0x98, 0x25, 0x5f, 0x55, 0xaf,
        0x99, 0xf6, 0x69, 0x4e, 0x3a, 0xdf, 0x8f, 0xed,
        0x59, 0x91, 0xaa, 0xff, 0x0b, 0xaa, 0x70, 0x8e,
        0x08, 0xfe, 0xa1, 0x32, 0xa9, 0xa7, 0xc6, 0xf9,
        0x1d, 0xfa, 0x52, 0x32, 0xc5, 0x5b, 0x64, 0x1e,
        0x75, 0x67, 0xe0, 0xac, 0xe0, 0x35, 0x9d, 0x4a,
        0x09, 0x00, 0x00, 0x00, 0x00, 0xb5, 0x9a, 0xf1,
        0xe2, 0x4a, 0xe8, 0xea, 0xed, 0x68, 0x15, 0xf4,
        0x04, 0x36, 0x96, 0x60, 0x48, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x5a, 0x2f, 0x2a, 0x16,
        0x6c, 0x8b, 0x34, 0x4e, 0xd0
    };

    esp_decrypt_cfg_t cfg = {
        .rsa_priv_key = (char *)rsa_private_pem_start,
        .rsa_priv_key_len = rsa_private_pem_end - rsa_private_pem_start,
    };
    esp_decrypt_handle_t ctx = esp_encrypted_img_decrypt_start(&cfg);
    TEST_ASSERT_NOT_NULL(ctx);

    pre_enc_decrypt_arg_t *args = calloc(1, sizeof(pre_enc_decrypt_arg_t));
    TEST_ASSERT_NOT_NULL(args);

    args->data_in = (char *)cipher;
    args->data_in_len = 521;

    esp_err_t err;
    err = esp_encrypted_img_decrypt_data(ctx, args);

    TEST_ESP_OK(err);

    err = esp_encrypted_img_decrypt_end(ctx);
    TEST_ESP_ERR(ESP_FAIL, err);
    if (args->data_out) {
        free(args->data_out);
    }
    free(args);
}

TEST_CASE("Sending random size data at once", "[encrypted_img]")
{
    esp_decrypt_cfg_t cfg = {
        .rsa_priv_key = (char *)rsa_private_pem_start,
        .rsa_priv_key_len = rsa_private_pem_end - rsa_private_pem_start,
    };
    esp_decrypt_handle_t ctx = esp_encrypted_img_decrypt_start(&cfg);
    TEST_ASSERT_NOT_NULL(ctx);

    pre_enc_decrypt_arg_t *args = calloc(1, sizeof(pre_enc_decrypt_arg_t));
    TEST_ASSERT_NOT_NULL(args);

    esp_err_t err;

    int i = 0;
    do {
        uint32_t y = esp_random();
        y = (y % 16) + 1;
        uint32_t x = y < ((bin_end - bin_start) - i) ? y : ((bin_end - bin_start) - i);
        args->data_in = (char *)(bin_start + i);
        i += x;
        args->data_in_len = x;
        err = esp_encrypted_img_decrypt_data(ctx, args);
        if (err == ESP_FAIL) {
            printf("ESP_FAIL ERROR\n");
            break;
        }
    } while (err != ESP_OK);

    TEST_ESP_OK(err);

    err = esp_encrypted_img_decrypt_end(ctx);
    TEST_ESP_OK(err);
    if (args->data_out) {
        free(args->data_out);
    }
    free(args);
}

TEST_CASE("Sending imcomplete data", "[encrypted_img]")
{
    esp_decrypt_cfg_t cfg = {
        .rsa_priv_key = (char *)rsa_private_pem_start,
        .rsa_priv_key_len = rsa_private_pem_end - rsa_private_pem_start,
    };
    esp_decrypt_handle_t ctx = esp_encrypted_img_decrypt_start(&cfg);
    TEST_ASSERT_NOT_NULL(ctx);

    pre_enc_decrypt_arg_t *args = calloc(1, sizeof(pre_enc_decrypt_arg_t));
    TEST_ASSERT_NOT_NULL(args);

    args->data_in = (char *)bin_start;
    args->data_in_len = (bin_end - bin_start) - 1;

    esp_err_t err;
    err = esp_encrypted_img_decrypt_data(ctx, args);
    TEST_ESP_ERR(ESP_ERR_NOT_FINISHED, err);

    TEST_ESP_ERR(false, esp_encrypted_img_is_complete_data_received(ctx));

    err = esp_encrypted_img_decrypt_abort(ctx);
    TEST_ESP_OK(err);
    if (args->data_out) {
        free(args->data_out);
    }
    free(args);
}

TEST_CASE("Test canceling decryption frees memory", "[encrypted_img]")
{
    esp_decrypt_cfg_t cfg = {
        .rsa_priv_key = (char *)rsa_private_pem_start,
        .rsa_priv_key_len = rsa_private_pem_end - rsa_private_pem_start,
    };
    int free_bytes_start = xPortGetFreeHeapSize();
    esp_decrypt_handle_t ctx = esp_encrypted_img_decrypt_start(&cfg);
    TEST_ASSERT_NOT_NULL(ctx);

    esp_err_t err = esp_encrypted_img_decrypt_abort(ctx);
    TEST_ESP_OK(err);
    int free_bytes_end = xPortGetFreeHeapSize();

    // +/- 16 bytes to allow for some small fluctuations
    TEST_ASSERT(abs(free_bytes_start - free_bytes_end) <= 16);
}
